package chatRoom.client;

import java.io.IOException;
import java.net.Socket;
import java.util.*;

import chatRoom.network.Connection;

public class ClientConnection extends Connection 
{	
	private Queue<String> outgoing;
	private Queue<String> incoming;
	
	public ClientConnection(Socket sock)
	{
		outgoing = new LinkedList<String>();
		incoming = new LinkedList<String>();
		setSocket(sock);
	}
	
	public void run() {
		while (!Thread.interrupted())
		{
			String input = "DED";
			try
			{
				input = getInput().readLine();
				incoming.add(input);
			} catch (IOException exception)
			{
				
			}
			
			if (input == null)
			{
				return;
			}
			
			if (!outgoing.isEmpty())
			{
				System.out.println(outgoing.peek());
				getOutput().print(outgoing.remove());
			}
		}
		
		try {
			getSocket().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Queue<String> getIn(){
		Queue<String> temp = incoming;
		incoming = new LinkedList<String>();
		return temp;
	}
	
	public void send(String message)
	{
		//outgoing.add("PRIVMSG 0 :" + message + "\n");
		outgoing.add(message);
	}
}
